<?php

namespace Facebook\WebDriver;

use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Pay\img;

require_once('vendor/autoload.php');

//检测是否需要验证码
function generateVcodeIMG($location, $size, $src_img)
{
    $width = $size->getWidth();
    $height = $size->getHeight();
    $x = $location->getX();
    $y = $location->getY();

    $src = imagecreatefrompng($src_img);
    $dst = imagecreatetruecolor($width, $height);
    imagecopyresampled($dst, $src, 0, 0, $x, $y, $width, $height, $width, $height);
    imagejpeg($dst, $src_img);
    chmod($src_img, 0777);
    imagedestroy($src);
    imagedestroy($dst);
}

function checkimg($driver)
{

    $img = $driver->findElement(WebDriverBy::id('J-checkcode-img'));
    $hid = $driver->findElement(WebDriverBy::id('J-checkcode'))->getAttribute("class");
    $check = preg_match('/fn-hide/', $hid);

    if (!$check) {
        $driver->manage()->window()->maximize();    //将浏览器最大化
        $driver->takeScreenshot("vedio.png");  //截取当前网页，该网页有我们需要的验证码
        $element = $driver->findElement(WebDriverBy::id('J-checkcode-img'));
        generateVcodeIMG($element->getLocation(), $element->getSize(), "vedio.png");
        $hid = $driver->findElement(WebDriverBy::id('J-checkcode'))->getAttribute("class");
        var_dump($hid);
        //正则匹配fn-hide
        if (!$check) {


            $app_id = 305603;
            $app_key = " BDK07osOtoHrWZ2EBI5+7s7I61wcWFnX";
//pd账号id&key
            $usr_id = 105603;
            $usr_key = "335YeT6HRUIyDMaJaf2C6/BMYwcHaD8V";
            echo 1;
            $a = json_decode((new img($app_id, $app_key, $usr_id, $usr_key))->deel("vedio.png")["RspData"], true);
            $a = $a["result"];
            echo $a;
            //输入J-input-checkcode
            $driver->findElement(WebDriverBy::id('J-input-checkcode'))->sendKeys($a);
        }
    } else {
        return false;
    }


}

header("Content-Type: text/html; charset=UTF-8");
// start Firefox with 5 second timeout
$waitSeconds = 15;  //需等待加载的时间，一般加载时间在0-15秒，如果超过15秒，报错。
$host = 'http://localhost:4444/wd/hub'; // this is the default
//这里使用的是chrome浏览器进行测试，需到http://www.seleniumhq.org/download/上下载对应的浏览器测试插件
//我这里下载的是win32 Google Chrome Driver 2.25版：https://chromedriver.storage.googleapis.com/index.html?path=2.25/
$capabilities = DesiredCapabilities::chrome();
$driver = RemoteWebDriver::create($host, $capabilities, 5000);
// navigate to 'http://docs.seleniumhq.org/'
$driver->get('https://www.alipay.com/');

$element = $driver->findElement(WebDriverBy::cssSelector('.title>.personal'));

$driver->executeScript("arguments[0].click();", [$element]);


// 等待新的页面加载完成....
$driver->wait(2)->until(
    WebDriverExpectedCondition::visibilityOfElementLocated(
        WebDriverBy::id('J_onepage')

    )
);
$element = $driver->findElement(WebDriverBy::cssSelector('.button-login'));

$driver->executeScript("arguments[0].click();", [$element]);

$driver->wait(2)->until(
    WebDriverExpectedCondition::visibilityOfElementLocated(
        WebDriverBy::id("J_loginIframe")

    )
);

//$element = $driver->findElement(WebDriverBy::cssSelector('#J_loginbox>.qrcode-target-hide'));
$driver->switchTo()->frame("J_loginIframe");
$str = $driver->getPageSource();
$element = $driver->findElement(WebDriverBy::cssSelector('.qrcode-target-hide'));

$driver->executeScript("arguments[0].click();", [$element]);

//获取验证码图片

//$img = $driver -> findElement(WebDriverBy::id('J-checkcode-img'));
//if($img){
//    $files = $img->getAttribute("src");
//}


//file_get_contents($files,"vedio.png");
//
//var_dump($img);


//输入账号密码
sleep(5);

$driver->findElement(WebDriverBy::id('J-input-user'))->sendKeys("346312616@qq.com");

sleep(5);

$driver->findElement(WebDriverBy::id('password_rsainput'))->sendKeys("19960521Aa");

sleep(5);


checkimg($driver);

sleep(2);
//点击
$driver->findElement(WebDriverBy::id('J-login-btn'))->click();


sleep(5);
if ($driver->getTitle() == "支付宝 知托付！") {
    $a = [1, 9, 9, 6, 0, 5, 2, 1, "A", "a"];
    foreach ($a as $key) {

        $driver->findElement(WebDriverBy::id('password_rsainput'))->sendKeys($key);

        echo $key;

        sleep(1);
    }
    checkimg($driver);


    $driver->findElement(WebDriverBy::id('J-login-btn'))->click();
}


//$img = $driver -> findElement(WebDriverBy::id('J-checkcode-img'));
//if($img){
//    $files = $img->getAttribute("src");
//}
sleep(3);

$driver->get("https://consumeprod.alipay.com/record/standard.htm");
//账单页面
//https://consumeprod.alipay.com/record/standard.htm
//for($i=0;$i<1000;$i++) {
//
//    sleep(40);
//
//    $driver->get("https://consumeprod.alipay.com/record/standard.htm");
//
//    echo $i."\n";
//}
//支付宝 - 网上支付 安全快速！
echo $driver->getTitle();


$http = new \swoole_http_server("127.0.0.1", 9501);

$http->on("start", function ($server) {
    echo "Swoole http server is started at http://127.0.0.1:9501\n";
});

$http->on("request", function ($request, $response) {
    $response->header("Content-Type", "text/plain");
    if ($request->get['test'] == 100) {
        global $driver;
        $driver->get("https://consumeprod.alipay.com/record/standard.htm");
        $response->end($driver->getTitle());

    }

});

$http->start();

//切换至最后一个window
//因为有些网站的链接点击过去带有target="_blank"属性，就新开了一个TAB，而selenium还是定位到老的TAB上，如果要实时定位到新的TAB，则需要调用此方法，切换到最后一个window
function switchToEndWindow($driver)
{

    $arr = $driver->getWindowHandles();
    foreach ($arr as $k => $v) {
        if ($k == (count($arr) - 1)) {
            $driver->switchTo()->window($v);
        }
    }
}

?>